package com.wyzc.exam.net;

import java.util.ArrayList;




import com.wyzc.exam.Exception.IdPassException;
import com.wyzc.exam.entity.ExamInfo;
import com.wyzc.exam.entity.QuestionInfo;
import com.wyzc.exam.entity.Request;
import com.wyzc.exam.entity.Response;
import com.wyzc.exam.entity.User;
import com.wyzc.exam.service.ExamService;

public class ExamServiceProxy implements ExamService{
    private String sessionId;
	@Override
	public User logon(String id, String pass) throws IdPassException {
		Request request = new Request(new Object[]{id, pass}, new Class[]{String.class, String.class}, "logon");
		Response response = SocketUtil.remoteAll(request);
		if(!response.isSuccess())
			throw new IdPassException(response.getException().getMessage());
		Object obj = response.getObj();
		this.sessionId = response.getSessionId();
		return (User)obj;
	}
    
	@Override
	public ExamInfo start() {
		Request request = new Request(new Object[]{}, new Class[]{}, "start");
		request.setSessionId(sessionId);
		Response response = SocketUtil.remoteAll(request);
		if(! response.isSuccess()) {
			throw new RuntimeException(response.getException().getMessage());
		}
		ExamInfo examInfo = (ExamInfo)response.getObj();
		
		return examInfo;
	}

	@Override
	public ArrayList<QuestionInfo> getExamQuestions() {
		Request request = new Request(new Object[]{}, new Class[]{}, "getExamQuestions");
		request.setSessionId(sessionId);
		Response response = SocketUtil.remoteAll(request);
		if(! response.isSuccess()) {
			throw new RuntimeException(response.getException().getMessage());
		}
		ArrayList<QuestionInfo> examQuestions = (ArrayList<QuestionInfo>)response.getObj();
		return examQuestions;
	}

	@Override
	public QuestionInfo getQuestionInfo(int pageIndex) {
		Request request = new Request(new Object[]{pageIndex}, new Class[]{int.class}, "getQuestionInfo");
		request.setSessionId(sessionId);
		Response response = SocketUtil.remoteAll(request);
		if(! response.isSuccess()) {
			throw new RuntimeException(response.getException().getMessage());
		}
		QuestionInfo questionInfo = (QuestionInfo)response.getObj();
		return questionInfo;
	}

	@Override
	public int getTotalScore() {
		Request request = new Request(new Object[]{}, new Class[]{}, "getTotalScore");
		request.setSessionId(sessionId);
		Response response = SocketUtil.remoteAll(request);
		if(! response.isSuccess()) {
			throw new RuntimeException(response.getException().getMessage());
		}
		Integer result = (Integer)response.getObj();
		return result;
	}

	@Override
	public void saveUserAnswers(int pageIndex, ArrayList<Integer> userAnswers) {
		Request request = new Request(new Object[]{pageIndex, userAnswers}, new Class[]{int.class, userAnswers.getClass()}, "saveUserAnswers");
		request.setSessionId(sessionId);
		Response response = SocketUtil.remoteAll(request);
		if(! response.isSuccess()) {
			throw new RuntimeException(response.getException().getMessage());
		}
	}
}
